﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Threading.Tasks;
using System.Xml.Linq;
using CoreShop.Model;
using CoreShop.Repository;
using SqlSugar;


namespace CoreShop.Services
{
    /// <summary>
    /// 接口实现
    /// </summary>
    public class corecmsfoodfeedcategoryServices : Repository<corecmsfoodfeedcategory>, IcorecmsfoodfeedcategoryServices
    {
        #region 实现重写增删改查操作==========================================================

        /// <summary>
        /// 重写异步插入方法
        /// </summary>
        /// <param name="entity">实体数据</param>
        /// <returns></returns>
        public bool InsertAsync(corecmsfoodfeedcategory entity)
        {
            return base.Change<corecmsfoodfeedcategory>().Insert(entity);
        }
        public bool InsertOrUpdate(List<corecmsfoodfeedcategory> entity)
        {
            return base.InsertOrUpdate(entity);
        }
        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(corecmsfoodfeedcategory entity)
        {
            return base.Change<corecmsfoodfeedcategory>().Update(a => new corecmsfoodfeedcategory()
            {
                corpId = entity.corpId,
                storeId = entity.storeId,
                name = entity.name,
                updateTime = entity.updateTime
            }, a => a.id == entity.id);
        }

        /// <summary>
        /// 重写异步更新方法方法
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateAsync(List<corecmsfoodfeedcategory> entity)
        {
            bool returnKey = true;
            foreach (var item in entity)
            {
                returnKey = base.Change<corecmsfoodfeedcategory>().Update(a => new corecmsfoodfeedcategory()
                {
                    corpId = item.corpId,
                    storeId = item.storeId,
                    name = item.name,
                    updateTime = item.updateTime
                }, a => a.id == item.id);
            }
            return returnKey;
        }

        /// <summary>
        /// 重写删除指定ID的数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteByIdAsync(long id)
        {
            return base.Change<corecmsfoodfeedcategory>().Update(a => new corecmsfoodfeedcategory()
            {
                isDelete = true,
                updateTime = DateTime.Now
            }, a => a.id == id);
        }

        /// <summary>
        /// 重写删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIdsAsync(int[] ids)
        {
            bool returnKey = true;
            foreach (int id in ids)
            {
                returnKey = base.Change<corecmsfoodfeedcategory>().Update(a => new corecmsfoodfeedcategory()
                {
                    isDelete = true,
                    updateTime = DateTime.Now
                }, a => a.id == id);
            }
            return returnKey;
        }

        #endregion

        #region 获取缓存的所有数据==========================================================

        /// <summary>
        /// 获取缓存的所有数据
        /// </summary>
        /// <returns></returns>
        public List<corecmsfoodfeedcategory> GetCaChe()
        {
            return base.GetList();
        }

        #endregion

        #region 根据条件查询分页数据
        /// <summary>
        /// 根据条件查询分页数据
        /// </summary>
        /// <param name="count"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="sName"></param>
        /// <returns></returns>
        public List<corecmsfoodfeedcategory> QueryPageAsync(ref int count, int? pageIndex, int? pageSize, string sName)
        {
            List<corecmsfoodfeedcategory> lst = new List<corecmsfoodfeedcategory>();
            var where = Expressionable.Create<corecmsfoodfeedcategory>();
            where = where.And(a => a.isDelete == false);
            if (!string.IsNullOrEmpty(sName))
            {
                where = where.And(a => a.name.Contains(sName))
                    .Or(a => a.name.Contains(sName));
            }
            if (pageIndex >= 1 && pageSize > 0)
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression())
                    .ToPageList((int)pageIndex, (int)pageSize, ref count)
                    .OrderBy(a => a.name).ToList();
            }
            else
            {
                lst = base.AsQueryable().WhereIF(true, where.ToExpression()).OrderBy(a => a.name).ToList();
            }

            return lst;
        }
        #endregion

    }
}
